﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace RoardersAgentSim
{
	public static class Utils
	{
		private static Random rnd = null;
        private static double M = double.PositiveInfinity;
        private static double _timeConversion = -1;
        private static bool _debug = false;
        private static string _output = null;
        private static double _distanceConversion = -1;
        private static string _csvDelimiter = string.Empty;
        private static double _carCongestionSpeed = -1;

        public static bool OutPutInitialised { get; set; }
        public static string FilePrefix { get; set; }
        /// <summary>
        /// Initialise the settings
        /// </summary>
		private static void Initialise()
		{
			if(rnd == null)
				rnd = new Random();
            if (M == double.PositiveInfinity)

                M = double.Parse(ConfigurationManager.AppSettings["LARGEM"]);
            if (_timeConversion < 0)
            {
                if (!ConfigurationManager.AppSettings.AllKeys.Contains("TIME_CONVERSION_UNIT"))
                    _timeConversion = 1;
                else
                    _timeConversion = double.Parse(ConfigurationManager.AppSettings["TIME_CONVERSION_UNIT"]);

                if (!ConfigurationManager.AppSettings.AllKeys.Contains("DEBUG"))
                {
                    _debug = false;

                }
                else
                {
                    if (string.Compare(ConfigurationManager.AppSettings["DEBUG"], "YES", StringComparison.InvariantCultureIgnoreCase) == 0)
                        _debug = true;
                }

            }
            if (_carCongestionSpeed < 0)
            {
                if (!ConfigurationManager.AppSettings.AllKeys.Contains("CAR_CONGESTION_SPEED"))
                    _carCongestionSpeed = 1;
                else
                    _carCongestionSpeed = double.Parse(ConfigurationManager.AppSettings["CAR_CONGESTION_SPEED"]);
            }

            if (string.IsNullOrEmpty(_output))
            {
                _output = ConfigurationManager.AppSettings["output"];
                if (!_output.EndsWith(@"\"))
                    _output += @"\";
            }
            if (string.IsNullOrEmpty(_csvDelimiter))
            {
                _csvDelimiter = ConfigurationManager.AppSettings["CSV_DELIMITER"]; 
            }
		}

        public static string GetCSVDelimiter()
        {
            Initialise();
            return _csvDelimiter;
        }

        public static bool IsInDebugMode()
        {
            Initialise();
            return _debug;
        }

        public static string GetOutputDirectory()
        {
            Initialise();
            return _output;
        }

		public static double NextDouble(double min, double max)
		{
			Initialise();
			return rnd.NextDouble() * (max - min) + min;
		}

        public static double GetLargeM()
        {
            Initialise();
            return M;
        }
        public static double GetCarCongestionSpeed()
        {
            Initialise();
            return _carCongestionSpeed;
        }
        internal static double GetTimeConversion()
        {
            Initialise();
            return _timeConversion;
        }
    }
}
